//
//  SourceListElementNetwork.m
//  SolarSeek
//
//  Created by Erick Fejta on 05/03/18.
//  Copyright 2005 Erick Fejta. All rights reserved.
//

#import "SourceListElementNetwork.h"
#import "SolarSeekApplication.h"
#import "Message.h"
#import "Connections.h"
#import "SolarSeekEngine.h"
#import "ServerConnectionController.h"

typedef enum { ObservingConnection, ObservingServerMessageReceived,ObservingServerMessageSent } ObservingTypes;
@implementation SourceListElementNetwork
- (id)initWithView:(NSView *)view {
	if(self = [super initWithView:view]) {
		SolarSeekEngine *engine = [(SolarSeekApplication *)NSApp engine];	

		[engine addObserver:self forKeyPath:@"serverConnectionController.loggedIn" options:nil context:(void *)ObservingConnection];
		[engine addObserver:self forKeyPath:@"serverConnection.messageReceived" options:nil context:(void *)ObservingServerMessageReceived];
		[engine addObserver:self forKeyPath:@"serverConnection.messageSent" options:nil context:(void *)ObservingServerMessageSent];

	}
	return self;
}

- (void)dealloc {
		SolarSeekEngine *engine = [(SolarSeekApplication *)NSApp engine];	

		[engine removeObserver:self forKeyPath:@"serverConnectionController.loggedIn"];
		[engine removeObserver:self forKeyPath:@"serverConnection.messageReceived"];
		[engine removeObserver:self forKeyPath:@"serverConnection.messageSent"];
	[super dealloc];
}

- (NSImage *)icon { return [NSImage imageNamed:@"sourcelistNetwork"]; }
- (NSString *)titleWindow { return @"Network Status"; }
- (NSString *)name { return [[[NSApp engine] serverConnectionController] loggedIn]?@"Connected":@"Disconnected"; }

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
	SolarSeekEngine *engine = [(SolarSeekApplication *)NSApp engine];
	switch((int)context) {
		case ObservingConnection: {
			if([[engine serverConnectionController] loggedIn]) {
				[NSApp appendLogWithHeader:@"SERVER" HTML:@"Logged In." type:LogTypeServer];
			}
			else {
				NSString *error = @"Disconnected.";
				NSString *reason = [[engine serverConnectionController] messageError];
				if(reason && [reason length] > 0)
					error = [error stringByAppendingString:reason];
				[NSApp appendLogWithHeader:@"SERVER" HTML:error type:LogTypeServer];
			}
			[self refreshAndRequestAttention:YES];
			break;
		}
		case ObservingServerMessageSent: {
			Message *m = [[engine serverConnection] messageSent];
			[m rewind];
//			[NSApp appendLogWithHTML:[NSString stringWithFormat:@"<b>SERVER:</b> Sent %d byte message with code %d.",[m countBytesRemaining],[m getCode]] type:LogTypeServer];
			break;
		}
		case ObservingServerMessageReceived: {
			Message *m = [[engine serverConnection] messageReceived];
			[m rewind];
//			[NSApp appendLogWithHTML:[NSString stringWithFormat:@"<b>SERVER:</b> Received %d byte message with code %d.",[m countBytesRemaining],[m getCode]] type:LogTypeServer];
			break;
		}
		default:
			[NSException raise:@"AdamSucksException" format:@"Learn how to observe this."];
	}
}


@end
